# coding=utf-8
import  tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
import os
import datetime

from tornado.web import RequestHandler
from tornado.options import define,options
from tornado.websocket import WebSocketHandler

define("port",default=8000 ,type=int)
class IndexHandler(RequestHandler):
    def get(self):
        self.write("get hello")
    def post(self):
        self.write("post hello")
class ChatHandler(WebSocketHandler):
    users = set()
    def open(self):
        self.users.add(self)
	for u in self.users:
	    print u 
	    print dir(u)
	    u.write_message("进入聊天室")
    def on_message(self,message):
        self.write_message(u"Your message was :"+message)
        #for u in self.users:
	#    u.write_message("byebye")
    def on_close(self):
        self.users.remove(self)
	for u in self.users:
	    u.write_message("离开聊天室")
    def check_origin(self,origin):
        return True
if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application([
    (r"/",IndexHandler),
    (r"/chat",ChatHandler),],
    static_path = os.path.join(os.path.dirname(__file__),"static"),
    template_path=os.path.join(os.path.dirname(__file__),"template"),debug=True)


    http_server = tornado.httpserver.HTTPServer(app)
    print options.port
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()









